package util;

import java.math.BigDecimal;

public class ConverterTool {

    public static boolean doubleIsInt(double num) {
        try {
            Boolean flag = false;// 没碰到小数点时候标记是false
            int n = 0;// 计数器
            char[] charArray = String.valueOf(num).toCharArray();
            for (char c : charArray) {
                if (c == '.') {
                    flag = true;
                    continue;
                }
                if (flag && Integer.valueOf(String.valueOf(c)) > 0) {
                    n++;
                }
            }
            if (n > 0) {
                return false;
            }
            return true;
        }catch (Exception ex){
            return false;
        }
    }

    public static Integer parseInt(String num) {
        if (num != null && !"".equals(num)) {
            Double d = Double.parseDouble(num);
            if (doubleIsInt(d)) {
                return d.intValue();
            }
        }
        return 0;
    }

    public static BigDecimal parseDecimal(String num){
        try {
            BigDecimal decimal=new BigDecimal(num);
            return decimal;
        }catch (Exception ex){
            return BigDecimal.valueOf(0);
        }
    }
}
